package com.example.hotel_manage.web.controller.employee;

import com.example.common.constant.ErrorCode;
import com.example.common.exception.BusinessException;
import com.example.common.pojo.vo.PageVO;
import com.example.hotel_manage.pojo.dto.employee.EmployeeAddDTO;
import com.example.hotel_manage.pojo.dto.employee.EmployeeUpdateDTO;
import com.example.hotel_manage.pojo.mapper.employee.EmployeeMapper;
import com.example.hotel_manage.pojo.po.employee.EmployeePO;
import com.example.hotel_manage.pojo.qo.employee.EmployeeQO;
import com.example.hotel_manage.pojo.vo.employee.EmployeeListVO;
import com.example.hotel_manage.pojo.vo.employee.EmployeeShowVO;
import com.example.hotel_manage.service.employee.EmployeeService;
import com.example.hotel_manage.web.AbstractController;
import com.example.hotel_manage.web.api.employee.EmployeeAPI;
import com.example.hotel_manage.web.constant.WebConst;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;

/**
 * 【员工】控制器
 */
@RestController
@RequestMapping(WebConst.ModulePath.EMPLOYEE + "/employee")
public class EmployeeController extends AbstractController implements EmployeeAPI {

    @Autowired
    private EmployeeService employeeService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<EmployeeShowVO> save(@Valid @RequestBody EmployeeAddDTO employeeAddDTO) throws Exception {
        EmployeePO employee = employeeService.save(employeeAddDTO);
        return ResponseEntity.created(new URI(WebConst.ModulePath.EMPLOYEE + "/employee/" + employee.getId()))
                .body(EmployeeMapper.INSTANCE.toShowVO(employee));
    }

    @Override
    @PutMapping
    public ResponseEntity<EmployeeShowVO> update(@Valid @RequestBody EmployeeUpdateDTO employeeUpdateDTO) {
        EmployeePO employee = employeeService.update(employeeUpdateDTO);
        return ResponseEntity.ok(EmployeeMapper.INSTANCE.toShowVO(employee));
    }

    @Override
    @GetMapping
    public ResponseEntity<PageVO<EmployeeListVO>> list(@Valid EmployeeQO employeeQO) {
        PageVO<EmployeeListVO> page = employeeService.list(employeeQO);
        return ResponseEntity.ok(page);
    }

    @Override
    @GetMapping(value = "/{id}")
    public ResponseEntity<EmployeeShowVO> show(@PathVariable Integer id) {
        EmployeeShowVO employeeShowVO = employeeService.show(id);
        return ResponseEntity.ok(employeeShowVO);
    }

    @Override
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Integer> delete(@PathVariable Integer id) {
        int count = employeeService.delete(id);
        return ResponseEntity.ok(count);
    }

    @Override
    @DeleteMapping
    public ResponseEntity<Integer> deleteBatch(@RequestBody Integer[] id) {
        if (ArrayUtils.isEmpty(id)) {
            throw new BusinessException(ErrorCode.PARAM_IS_NULL);
        }
        int count = employeeService.delete(id);
        return ResponseEntity.ok(count);
    }

}


